package string;

public class MyStringBuffer {
    private char[] value; // 存储字符的数组
    private int length;   // 当前字符长度

    // 构造方法
    public MyStringBuffer() {
        this(16); // 默认初始容量16
    }

    public MyStringBuffer(int capacity) {
        value = new char[capacity];
        length = 0;
    }

    public MyStringBuffer(String str) {
        this(str.length() + 16); // 初始容量为字符串长度+16
        append(str);
    }

    // 获取当前长度
    public int length() {
        return length;
    }

    // 获取当前容量
    public int capacity() {
        return value.length;
    }

    // 确保容量足够，不够则扩容
    private void ensureCapacity(int minCapacity) {
        if (minCapacity > value.length) {
            // 新容量为原来的2倍+2，或至少为minCapacity
            int newCapacity = value.length * 2 + 2;
            if (newCapacity < minCapacity) {
                newCapacity = minCapacity;
            }
            // 复制到新数组
            char[] newValue = new char[newCapacity];
            System.arraycopy(value, 0, newValue, 0, length);
            value = newValue;
        }
    }

    // 追加字符串
    public MyStringBuffer append(String str) {
        if (str == null) {
            str = "null";
        }
        int len = str.length();
        ensureCapacity(length + len);
        // 将str的字符复制到value数组
        str.getChars(0, len, value, length);
        length += len;
        return this; // 支持链式调用
    }

    // 追加单个字符
    public MyStringBuffer append(char c) {
        ensureCapacity(length + 1);
        value[length++] = c;
        return this;
    }

    // 插入字符串
    public MyStringBuffer insert(int offset, String str) {
        if (offset < 0 || offset > length) {
            throw new StringIndexOutOfBoundsException(offset);
        }
        if (str == null) {
            str = "null";
        }
        int len = str.length();
        ensureCapacity(length + len);
        // 移动原有字符，腾出空间
        System.arraycopy(value, offset, value, offset + len, length - offset);
        // 插入新字符
        str.getChars(0, len, value, offset);
        length += len;
        return this;
    }

    // 删除指定范围的字符
    public MyStringBuffer delete(int start, int end) {
        if (start < 0 || end > length || start > end) {
            throw new StringIndexOutOfBoundsException();
        }
        int len = end - start;
        // 移动字符覆盖被删除部分
        System.arraycopy(value, start + len, value, start, length - end);
        length -= len;
        return this;
    }

    // 反转字符串
    public MyStringBuffer reverse() {
        for (int i = 0; i < length / 2; i++) {
            char temp = value[i];
            value[i] = value[length - 1 - i];
            value[length - 1 - i] = temp;
        }
        return this;
    }

    // 转为字符串
    @Override
    public String toString() {
        return new String(value, 0, length);
    }

    // 测试自定义MyStringBuffer
    public static void main(String[] args) {
        MyStringBuffer msb = new MyStringBuffer("Hello");
        msb.append(", ").append("World").append('!');
        System.out.println(msb); // 输出: Hello, World!

        msb.insert(5, " Java");
        System.out.println(msb); // 输出: Hello Java, World!

        msb.delete(5, 10);
        System.out.println(msb); // 输出: Hello, World!

        msb.reverse();
        System.out.println(msb); // 输出: !dlroW ,olleH

        System.out.println("长度: " + msb.length());
        System.out.println("容量: " + msb.capacity());
    }
}
